home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / comm / uucp / AM114src.lha / getaddress.c < prev    next >
C/C++ Source or Header  |  1992-05-20  |  12KB  |  509 lines

  1. /*
  2.  *
  3.  *  AM --- AmigaMail
  4.  *  (C) 1991, 1992, 1992 by Christian Riede
  5.  *
  6.  *  AM is distributed in the hope that it will be useful, but WITHOUT ANY
  7.  *  WARRANTY.  No author or distributor accepts responsibility to anyone
  8.  *  for the consequences of using it or for whether it serves any
  9.  *  particular purpose or works at all, unless he says so in writing.
  10.  *  Refer to the GNU General Public License, Version 1, for full details.
  11.  *  
  12.  *  Everyone is granted permission to copy, modify and redistribute AM,
  13.  *  but only under the conditions described in the GNU General Public
  14.  *  License, Version 1.  A copy of this license is supposed to have been 
  15.  *  given to you along with AM so you can know your rights and responsi-
  16.  *  bilities.  It should be in a file named COPYING.  Among other things,
  17.  *  the copyright notice and this notice must be preserved on all copies.
  18.  *
  19.  *  
  20.  *
  21.  */
  22.  
  23.  
  24. #include "am.h"
  25.  
  26. #define TO 101
  27. #define CC 102
  28. #define BCC 103
  29. #define SUBJECT 104
  30. #define OK 105
  31. #define CANCEL 106
  32.  
  33. /* reentrant */
  34. int GetAddress(char *To,char *Cc, char *Bcc, char *Subject)
  35. {
  36.     int run,ok;
  37.     struct IntuiMessage *Msg;
  38.     ULONG Class;
  39.     ULONG Code;
  40.     APTR IAddr;
  41.     struct Gadget *GList,*gad,*ToGad,*CcGad,*BccGad,*SubjGad,*ListGad,*ActiveGad;
  42.     struct Window *ABWindow=0;
  43.     struct List Addressbook;
  44.     int LastSelected=-1;
  45.     struct Address *Address;
  46.     char Buffer[MAIL_FIELDLENGTH];
  47.  
  48.     struct NewGadget ABListGadget = 
  49.     {
  50.         0,0,0,0,
  51.         0 /*(UBYTE *)"To:"*/ ,&TextAttr,
  52.         LIST,
  53.         PLACETEXT_ABOVE,
  54.         0,    
  55.         0
  56.     };
  57.     
  58.     struct NewGadget ToGadget = 
  59.     {
  60.         0,0,0,0,
  61.         (UBYTE *)"To:",&TextAttr,
  62.         TO,
  63.         PLACETEXT_LEFT,
  64.         0,    
  65.         0
  66.     };
  67.     
  68.     struct NewGadget CcGadget = 
  69.     {
  70.         0,0,0,0,
  71.         (UBYTE *)"Cc:",&TextAttr,
  72.         CC,
  73.         PLACETEXT_LEFT,
  74.         0,    
  75.         0
  76.     };
  77.     
  78.     struct NewGadget BccGadget = 
  79.     {
  80.         0,0,0,0,
  81.         (UBYTE *)"Bcc:",&TextAttr,
  82.         BCC,
  83.         PLACETEXT_LEFT,
  84.         0,    
  85.         0
  86.     };
  87.     
  88.     struct NewGadget SubjectGadget = 
  89.     {
  90.         0,0,0,0,
  91.         (UBYTE *)"Subject:",&TextAttr,
  92.         SUBJECT,
  93.         PLACETEXT_LEFT,
  94.         0,    
  95.         0
  96.     };
  97.     
  98.     struct NewGadget OKGadget = 
  99.     {
  100.         0,0,0,0,
  101.         (UBYTE *)"Ok",&TextAttr,
  102.         OK,
  103.         PLACETEXT_IN,
  104.         0,    
  105.         0
  106.     };
  107.     
  108.     struct NewGadget CANCELGadget = 
  109.     {
  110.         0,0,0,0,
  111.         (UBYTE *)"Cancel",&TextAttr,
  112.         CANCEL,
  113.         PLACETEXT_IN,
  114.         0,    
  115.         0
  116.     };
  117.  
  118.     if (Screen) /* custom screen requested */
  119.     {
  120.         if (!(ABWindow = OpenWindowTags(0,
  121.             WA_CustomScreen,Screen,
  122.             WA_Left,ABLeft,
  123.             WA_Top,ABTop,
  124.             WA_Width,ABWidth,
  125.             WA_Height,(Screen->Height<360)?(ABHeight1):(ABHeight2),
  126.             WA_MinWidth,640L,
  127.             WA_MinHeight,180L,
  128.             WA_MaxWidth,1024L,
  129.             WA_MaxHeight,1024L,
  130.             WA_DragBar,TRUE,
  131.             WA_DepthGadget,TRUE,
  132.             WA_CloseGadget,TRUE,
  133.             WA_Activate,TRUE,
  134.             WA_SmartRefresh,TRUE,
  135.             WA_GimmeZeroZero,TRUE,
  136.             WA_RMBTrap,TRUE,
  137.             TAG_END)))
  138.         {
  139.             FreeAddressBook(&Addressbook);
  140.             SimpleRequest(Window,"Can't open Addressbook-Window");
  141.             return(FALSE);
  142.         }
  143.     }
  144.     else if (PubScreen) /* public screen requested */
  145.     {
  146.         if (!(ABWindow = OpenWindowTags(0,
  147.             WA_PubScreen,(UBYTE *)PubScreen,
  148.             WA_Left,ABLeft,
  149.             WA_Top,ABTop,
  150.             WA_Width,ABWidth,
  151.             WA_Height,(PubScreen->Height<360)?(ABHeight1):(ABHeight2),
  152.             WA_MinWidth,640L,
  153.             WA_MinHeight,180L,
  154.             WA_MaxWidth,1024L,
  155.             WA_MaxHeight,1024L,
  156.             WA_DragBar,TRUE,
  157.             WA_DepthGadget,TRUE,
  158.             WA_CloseGadget,TRUE,
  159.             WA_Activate,TRUE,
  160.             WA_SmartRefresh,TRUE,
  161.             WA_GimmeZeroZero,TRUE,
  162.             WA_RMBTrap,TRUE,
  163.             TAG_END)))
  164.         {
  165.             FreeAddressBook(&Addressbook);
  166.             SimpleRequest(Window,"Can't open Addressbook-Window");
  167.             return(FALSE);
  168.         }
  169.         
  170.     }
  171.     else
  172.         return(FALSE);
  173.  
  174.     /* read Addressbook */
  175.     ReadAddressBook(&Addressbook);
  176.  
  177.     /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
  178.  
  179.     GList = gad = 0;
  180.  
  181.     /* get visualinfo */
  182.     if(!(ABListGadget.ng_VisualInfo = GetVisualInfo(ABWindow->WScreen,TAG_END)))
  183.     {
  184.         SimpleRequest(Window,"Can't get visual info");
  185.         FreeAddressBook(&Addressbook);
  186.         CloseWindow(ABWindow);
  187.         return(FALSE);
  188.     }
  189.  
  190.     ToGadget.ng_VisualInfo = ABListGadget.ng_VisualInfo;
  191.     CcGadget.ng_VisualInfo = ABListGadget.ng_VisualInfo;
  192.     BccGadget.ng_VisualInfo = ABListGadget.ng_VisualInfo;
  193.     SubjectGadget.ng_VisualInfo = ABListGadget.ng_VisualInfo;
  194.     OKGadget.ng_VisualInfo = ABListGadget.ng_VisualInfo;
  195.     CANCELGadget.ng_VisualInfo = ABListGadget.ng_VisualInfo;
  196.  
  197.     /* allocate context */
  198.     if (!(gad = CreateContext(&GList)))
  199.     {
  200.             CloseWindow(ABWindow);
  201.             FreeAddressBook(&Addressbook);
  202.             SimpleRequest(Window,"Can't create context");
  203.             return(TRUE);
  204.     }
  205.  
  206.     /* adjust gadgets to window dimensions */
  207.     OKGadget.ng_LeftEdge = 5;
  208.     OKGadget.ng_TopEdge = ABWindow->GZZHeight-TextAttr.ta_YSize-8;
  209.     OKGadget.ng_Width = 100;                           /* !!!!!!!!!!!!!!! */
  210.     OKGadget.ng_Height = TextAttr.ta_YSize+4;
  211.  
  212.     CANCELGadget.ng_LeftEdge = ABWindow->GZZWidth-OKGadget.ng_Width-5;
  213.     CANCELGadget.ng_TopEdge = OKGadget.ng_TopEdge;
  214.     CANCELGadget.ng_Width = OKGadget.ng_Width;
  215.     CANCELGadget.ng_Height = OKGadget.ng_Height;
  216.  
  217.     SubjectGadget.ng_LeftEdge = 100;
  218.     SubjectGadget.ng_TopEdge = OKGadget.ng_TopEdge-TextAttr.ta_YSize-6;
  219.     SubjectGadget.ng_Width = ABWindow->GZZWidth-5-100;
  220.     SubjectGadget.ng_Height = TextAttr.ta_YSize+4;
  221.  
  222.     BccGadget.ng_LeftEdge = 100;
  223.     BccGadget.ng_TopEdge = SubjectGadget.ng_TopEdge-TextAttr.ta_YSize-6;
  224.     BccGadget.ng_Width = SubjectGadget.ng_Width;
  225.     BccGadget.ng_Height = TextAttr.ta_YSize+4;
  226.  
  227.     CcGadget.ng_LeftEdge = 100;
  228.     CcGadget.ng_TopEdge = BccGadget.ng_TopEdge-TextAttr.ta_YSize-6;
  229.     CcGadget.ng_Width = SubjectGadget.ng_Width;
  230.     CcGadget.ng_Height = TextAttr.ta_YSize+4;
  231.  
  232.     ToGadget.ng_LeftEdge = 100;
  233.     ToGadget.ng_TopEdge = CcGadget.ng_TopEdge-TextAttr.ta_YSize-6;
  234.     ToGadget.ng_Width = SubjectGadget.ng_Width;
  235.     ToGadget.ng_Height = TextAttr.ta_YSize+4;
  236.  
  237.     ABListGadget.ng_LeftEdge = 5;
  238.     ABListGadget.ng_TopEdge = 5 /*+TextAttr.ta_YSize*/;
  239.     ABListGadget.ng_Width = ABWindow->GZZWidth-10;
  240.     ABListGadget.ng_Height = ToGadget.ng_TopEdge-ABListGadget.ng_TopEdge-5;
  241.  
  242.     /* create OK gadget */
  243.     if(!(gad = CreateGadget(BUTTON_KIND,gad,&OKGadget,
  244.         GA_RELVERIFY,TRUE,
  245.         TAG_END)))
  246.     {
  247.             CloseWindow(ABWindow);
  248.             FreeAddressBook(&Addressbook);
  249.             SimpleRequest(Window,"Can't create ok gadget");
  250.             return(TRUE);
  251.     }
  252.  
  253.  
  254.     /* create CANCEL gadget */
  255.     if(!(gad = CreateGadget(BUTTON_KIND,gad,&CANCELGadget,
  256.         GA_RELVERIFY,TRUE,
  257.         TAG_END)))
  258.     {
  259.             FreeAddressBook(&Addressbook);
  260.             SimpleRequest(Window,"Can't create cancel gadget");
  261.             return(TRUE);
  262.     }
  263.  
  264.     /* get ToGadget */
  265.     if (!(ToGad = gad = CreateGadget(STRING_KIND,gad,&ToGadget,
  266.         GTST_MaxChars,MAIL_FIELDLENGTH,
  267.         GA_TabCycle,TRUE,
  268.         GA_Immediate,TRUE,
  269.         TAG_END)))
  270.     {
  271.             CloseWindow(ABWindow);
  272.             FreeAddressBook(&Addressbook);
  273.             SimpleRequest(Window,"Can't create To gadget");
  274.             return(TRUE);
  275.     }
  276.  
  277.     /* get CcGadget */
  278.     if (!(CcGad = gad = CreateGadget(STRING_KIND,gad,&CcGadget,
  279.         GTST_MaxChars,MAIL_FIELDLENGTH,
  280.         GA_TabCycle,TRUE,
  281.         GA_Immediate,TRUE,
  282.         TAG_END)))
  283.     {
  284.             CloseWindow(ABWindow);
  285.             FreeAddressBook(&Addressbook);
  286.             SimpleRequest(Window,"Can't create Cc gadget");
  287.             return(TRUE);
  288.     }
  289.  
  290.     /* get BccGadget */
  291.     if (!(BccGad = gad = CreateGadget(STRING_KIND,gad,&BccGadget,
  292.         GTST_MaxChars,MAIL_FIELDLENGTH,
  293.         GA_TabCycle,TRUE,
  294.         GA_Immediate,TRUE,
  295.         TAG_END)))
  296.     {
  297.             CloseWindow(ABWindow);
  298.             FreeAddressBook(&Addressbook);
  299.             SimpleRequest(Window,"Can't create Bcc gadget");
  300.             return(TRUE);
  301.     }
  302.  
  303.     /* get SubjectGadget */
  304.     if (!(SubjGad = gad = CreateGadget(STRING_KIND,gad,&SubjectGadget,
  305.         GTST_MaxChars,MAIL_FIELDLENGTH,
  306.         GA_TabCycle,TRUE,
  307.         GA_Immediate,TRUE,
  308.         TAG_END)))
  309.     {
  310.             CloseWindow(ABWindow);
  311.             FreeAddressBook(&Addressbook);
  312.             SimpleRequest(Window,"Can't create To gadget");
  313.             return(TRUE);
  314.     }
  315.  
  316.     /* get listview-gadget */
  317.     if (!(ListGad = gad = CreateGadget(LISTVIEW_KIND,gad,&ABListGadget,
  318.         GTLV_Labels,&Addressbook,
  319. /*        GTLV_ShowSelected,ToGad,*/
  320.         TAG_END)))
  321.     {
  322.             CloseWindow(ABWindow);
  323.             FreeAddressBook(&Addressbook);
  324.             SimpleRequest(Window,"Can't create listview gadget");
  325.             return(TRUE);
  326.     }
  327.  
  328.     /* show up gadgets */
  329.     AddGList(ABWindow,GList,-1,-1,NULL);
  330.     RefreshGList(GList,ABWindow,NULL,-1);
  331.     GT_RefreshWindow(ABWindow,0);
  332.  
  333.     ModifyIDCMP(ABWindow,GADGETUP|GADGETDOWN|LISTVIEWIDCMP|CLOSEWINDOW);
  334.  
  335.     ActivateGadget(ToGad,ABWindow,0);
  336.     ActiveGad = ToGad;
  337.  
  338.     /* main loop */
  339.     run = TRUE;
  340.     ok = FALSE;
  341.     while (run)
  342.     {
  343.         Wait(1L<<ABWindow->UserPort->mp_SigBit);
  344.         while (Msg = GT_GetIMsg(ABWindow->UserPort))
  345.         {
  346.             /* remember values */
  347.             Class = Msg->Class;
  348.             Code  = Msg->Code;    
  349.             IAddr = Msg->IAddress;
  350.  
  351.             /* reply message */
  352.             GT_ReplyIMsg(Msg);
  353.  
  354.             /* dispatch */
  355.             switch(Class)
  356.             {
  357.                 case CLOSEWINDOW:
  358.                     strcpy(To,"");
  359.                     run = FALSE;
  360.                     ok = FALSE;
  361.                     break;
  362.  
  363.                 case REFRESHWINDOW:
  364.                     GT_BeginRefresh(ABWindow);
  365.                     GT_EndRefresh(ABWindow,TRUE);
  366.                     break;
  367.  
  368.                 case GADGETUP:
  369.                     switch(((struct Gadget *)IAddr)->GadgetID)
  370.                     {
  371.                         case LIST: /* user selected item */
  372.                             LastSelected = Code;
  373.                             if (Address = (struct Address *)
  374.                                 findnode(&Addressbook,Code));
  375.                             {
  376.                                 if (strlen((char *)((struct StringInfo *)
  377.                                     ActiveGad->SpecialInfo)->Buffer)>MAIL_FIELDLENGTH)
  378.                                     DisplayBeep(0L);
  379.                                 else
  380.                                 {
  381.                                     if (ActiveGad)
  382.                                     {
  383.                                         sprintf(Buffer,"\"%s\" <%s>",
  384.                                             /*(char *)((struct StringInfo *)
  385.                                                 ActiveGad->SpecialInfo)->Buffer,*/
  386.                                             Address->Realname,
  387.                                             Address->Username);
  388.                                         GT_SetGadgetAttrs(ActiveGad,ABWindow,0,
  389.                                             GTST_String,(ULONG)Buffer,
  390.                                             TAG_END,0);
  391.                                         ActivateGadget(ActiveGad,ABWindow,0);
  392.                                     }
  393.                                 }
  394.                             }
  395.                             break;
  396.  
  397.                         case CANCEL:
  398.                             strcpy(To,"");
  399.                             strcpy(Cc,"");
  400.                             strcpy(Bcc,"");
  401.                             strcpy(Subject,"");
  402.                             run = FALSE;
  403.                             ok = FALSE;
  404.                             break;
  405.  
  406.                         case SUBJECT:
  407.                             if (Code==0x09) /* GA_TabCycle */
  408.                             {
  409.                                 ActiveGad = ToGad;
  410.                                 break;
  411.                             } /* else go on with OK */
  412.  
  413.                         case OK:
  414.                             strcpy(To,(char *)((struct StringInfo *)ToGad->SpecialInfo)->Buffer);
  415.                             strcpy(Cc,(char *)((struct StringInfo *)CcGad->SpecialInfo)->Buffer);
  416.                             strcpy(Bcc,(char *)((struct StringInfo *)BccGad->SpecialInfo)->Buffer);
  417.                             strcpy(Subject,(char *)((struct StringInfo *)SubjGad->SpecialInfo)->Buffer);
  418.                             if (!*To)
  419.                             {
  420.                                 SimpleRequest(ABWindow,"No To: address selected!");
  421.                                 ActivateGadget(ToGad,ABWindow,0);
  422.                             }
  423.                             else if (!*Subject)
  424.                             {
  425.                                 SimpleRequest(ABWindow,"Please Enter a Subject");
  426.                                 ActivateGadget(SubjGad,ABWindow,0);
  427.                             }
  428.                             else 
  429.                             {
  430.                                 run = FALSE;
  431.                                 ok = TRUE;
  432.                             }
  433.                             break;
  434.  
  435.                         case TO:
  436.                             ActivateGadget(CcGad,ABWindow,0);
  437.                             ActiveGad = CcGad;
  438.                             break;
  439.  
  440.                         case CC:
  441.                             ActivateGadget(BccGad,ABWindow,0);
  442.                             ActiveGad = BccGad;
  443.                             break;
  444.  
  445.                         case BCC:
  446.                             ActivateGadget(SubjGad,ABWindow,0);
  447.                             ActiveGad = SubjGad;
  448.                             break;
  449.  
  450.                         default:
  451.                             puts("unknown gadget");
  452.                     }
  453.  
  454.                 case GADGETDOWN:
  455.                     switch(((struct Gadget *)IAddr)->GadgetID)
  456.                     {
  457.                         case TO:
  458.                         case CC:
  459.                         case BCC:
  460.                         case SUBJECT:
  461.                             ActiveGad = (struct Gadget *)IAddr;
  462.                             break;
  463.  
  464.                         case LIST:
  465.                             break;
  466.  
  467.                         default:
  468.                             ActiveGad = 0;
  469.                             break;
  470.                     }
  471.                     break;
  472.  
  473.                 case MOUSEBUTTONS:
  474.                     break;
  475.                 case INTUITICKS:
  476.                     break;
  477.                 default:
  478.                     printf("Unknkown IntuiMessage->Class:%x\n",Class);
  479.                     break;
  480.             }
  481.         }
  482.     }
  483.     
  484.     /* get to, subject */
  485.     strcpy(To,(char *)((struct StringInfo *)ToGad->SpecialInfo)->Buffer);
  486.     strcpy(Subject,(char *)((struct StringInfo *)SubjGad->SpecialInfo)->Buffer);
  487.  
  488.     /* remove,free gadgets */
  489.     RemoveGList(ABWindow,GList,-1); 
  490.     FreeGadgets(GList);
  491.  
  492.     if (!ok)
  493.     {
  494.         FreeVisualInfo(ABListGadget.ng_VisualInfo);
  495.         CloseWindow(ABWindow);
  496.         return(FALSE);
  497.     }
  498.  
  499.     FreeVisualInfo(ABListGadget.ng_VisualInfo);
  500.  
  501.     CloseWindow(ABWindow);
  502.  
  503.     /* free assressbook */
  504.     FreeAddressBook(&Addressbook);
  505.  
  506.     return(TRUE);
  507. }
  508.  
  509.